module.exports = {
	getStoreById: getStoreById,
	sendSMSToMember: sendSMSToMember,
	getMstoreSettings: getStoreByDomainName,
	getStoreByDomainName: getStoreByDomainName,
	getStoreConfig: getStoreConfig
};

var db = require('../common/database/db');
var http = require('http');
var idHelper = require('../common/utils/id-helper');

function getStoreConfig(storeId, key, cb) {
	db.queryWithParamSingle("select * from store_config where store_id=? and `key`=?", [storeId, key], cb);
}

function getStoreById(storeId, user, cb) {
	db.queryWithParamSingle("select *, modules from store where id=?", [storeId], cb);
}
function getMstoreSettings(storeId, cb) {
	db.queryWithParamSingle("select * from mstore_setting where store_id=? or ", [storeId], cb);
}
function sendSMSToMember(code, phone, merchant, cb, msg) {
	var options = idHelper.getSendSMSOpinions('验证码：' + code + "," + merchant.name + '会员动态登录密码,五分钟内有效。【门铺集】', phone);
	if (msg) {
		options = idHelper.getSendSMSOpinions(msg, phone);
	}
	var data = options.data;
	delete  options.data;
	var req = http.request(options, function (res) {
		res.on('data', function (chunk) {
		});
	});
	req.on('error', function (e) {
		console.log(e);
	});
	req.write(data);

	cb();
	req.end();
}

function getStoreByDomainName(domainName, cb) {
	db.queryWithParamSingle("select * from mstore_setting where domain_name=? or store_id=?", [domainName, domainName], function (err, settings) {
		if (settings && settings.banner_image) {
			if (process.env.NODE_ENV == "test") {
				settings.banner_image = "http://localhost:9090/api/stores/" + settings.store_id + "/mstoresetting/pic/bin";
			} else {
				settings.banner_image = "http://app.menpuji.com/api/stores/" + settings.store_id + "/mstoresetting/pic/bin";
			}
		}
		cb(err, settings);
	});
}
